<?php

class User extends CActiveRecord {
    /**
     * The followings are the available columns in table 'z_user':
     * @var integer $id_user
     * @var string $name
     * @var string $password
     * @var string $c_date
     * @var string $u_date
     * @var string $i_date
     * @var string $b_date
     * @var string $mail
     * @var integer $access
     */
    public $password2;
    public $verifyCode;
    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    public function behaviors() {
        return array(
                'AutoTimestampBehavior' => array(
                        'class' => 'application.components.AutoTimestampBehavior',
                // вы можете указать дополнительные опции здесь
                )
        );
    }
    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'z_user';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
                array('name, password, password2, mail', 'required'),
                array('access, activation', 'numerical', 'integerOnly'=>true),
                array('c_date, u_date, i_date, b_date', 'safe'),
                array('verifyCode', 'captcha', 'allowEmpty' => !extension_loaded('gd'), 'captchaAction' => 'site/captcha'
                        ,'on'=>'registration'),
                array('password', 'compare', 'compareAttribute'=>'password2'),
                array('mail', 'email'),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id_user, name, password, c_date, u_date, i_date, b_date, mail, access, activation', 'safe', 'on'=>'search'),
        );
    }


    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    public function safeAttributes() {

        return array('registration' => 'name, password, password2, mail, verifyCode',);
    }

    public function checkName($name) {
        return User::model()->exists(array(
                'condition'=>'name=:name',
                'params'=>array(':name'=>$name),
        ));
    }

    public function checkMail($name) {
        return User::model()->exists(array(
                'condition'=>'mail=:name',
                'params'=>array(':name'=>$name),
        ));
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
                'id_user' => 'id',
                'name' => 'Логин',
                'password' => 'Пароль',
                'password2' => 'Повтор пароля',
                'verifyCode'=>'Проверочный код',
                'c_date' => 'Дата создания',
                'u_date' => 'Дата изменения',
                'i_date' => 'Дата последней авторизаций',
                'b_date' => 'Дата до какого забанен',
                'mail' => 'Mail',
                'access' => 'Access',
                'activation'=>'activation',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id_user',$this->id_user);

        $criteria->compare('name',$this->name,true);

        $criteria->compare('password',$this->password,true);

        $criteria->compare('c_date',$this->c_date,true);

        $criteria->compare('u_date',$this->u_date,true);

        $criteria->compare('i_date',$this->i_date,true);

        $criteria->compare('b_date',$this->b_date,true);

        $criteria->compare('mail',$this->mail,true);

        $criteria->compare('access',$this->access);

        return new CActiveDataProvider('User', array(
                        'criteria'=>$criteria,
        ));
    }
}